using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 自制时间类
/// </summary>
public class SelfTime
{
    static SelfTime ins;

    public static SelfTime instance
    {
        get
        {
            if (ins == null)
                ins = new SelfTime();
            return ins;
        }
    }


    /// <summary>
    /// GMT(格林威治时间)时间转成本地时间
    /// </summary>
    /// <param name="gmt">字符串形式的GMT时间</param>
    /// <returns></returns>
    public static DateTime FormattingGMT(string gmt)
    {
        DateTime dt = DateTime.MinValue;
        try
        {
            string pattern = "";
            if (gmt.IndexOf("+0") != -1)
            {
                gmt = gmt.Replace("GMT", "");
                pattern = "ddd, dd MMM yyyy HH':'mm':'ss zzz";
            }

            if (gmt.ToUpper().IndexOf("GMT") != -1)
            {
                pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
            }

            if (pattern != "")
            {
                dt = DateTime.ParseExact(gmt, pattern, System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.AdjustToUniversal);
                dt = dt.ToLocalTime();
            }
            else
            {
                dt = Convert.ToDateTime(gmt);
            }
        }
        catch (Exception e)
        {
            Debug.Log(e);
            Debug.LogError("时间转换错误...");
        }
        return dt;
    }



    public static long AddTimestamps(long timestamp1, long timestamp2)
    {
        // 将两个时间戳相加
        long resultTimestamp = timestamp1 + timestamp2;

        return resultTimestamp;
    }

    /*    /// <summary>
        /// 时间戳转日期
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static  DateTime GetDateTimeByTimeStamp(double timeStamp)
        {
            DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
            DateTime dt = startTime.AddSeconds(timeStamp);
            return dt;
        }*/
    /// <summary>
    /// 时间戳转日期（毫秒)
    /// </summary>
    /// <param name="timeStamp"></param>
    /// <returns></returns>
    public static DateTime GetDateTimeByTimeMStamp(long timeStamp)
    {
        //Debug.Log("转换毫秒级时间戳" + timeStamp);
        // DateTime dateTime = DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).DateTime;
        DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
        DateTime dt = startTime.AddMilliseconds(timeStamp);
        return dt;
    } 
    /// <summary>
    /// 时间戳转日期（秒)
    /// </summary>
    /// <param name="timeStamp"></param>
    /// <returns></returns>
    public static DateTime GetDateTimeByTimeSStamp(long timeStamp)
    {
        //Debug.Log("转换毫秒级时间戳" + timeStamp);
        // DateTime dateTime = DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).DateTime;
        DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
        DateTime dt = startTime.AddSeconds(timeStamp);
        return dt;
    }

    /// <summary>
    /// 获取时间戳
    /// </summary>
    /// <returns></returns>
    public static long GetTimeStamp()
    {
        TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return Convert.ToInt64(ts.TotalSeconds);
    }

    /// <summary>
    /// 得到某天24点时间戳
    /// </summary>
    /// <returns></returns>
    public long GetTo24TimeStamp(DateTime date)
    {
        DateTime the24Time = Convert.ToDateTime(date.AddDays(1).ToString("D").ToString()).AddSeconds(-1);
        TimeSpan ts = the24Time - DateTime.Now;
        return (long)ts.TotalSeconds;
    }

    /// <summary>
    /// 传入当前时间戳，得到24点时间戳
    /// </summary>
    /// <param name="timeStamp"></param>
    /// <returns></returns>
    public long GetTo24TimeStampByTimeStamp(long timeStamp)
    {
        DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        DateTime dt = dateTime.AddSeconds(timeStamp);
        long res = GetTo24TimeStamp(dt);
        Debug.Log("举例24点还有" + res);
        return res;
    }

    /// <summary>
    ///  将DateTime转换为时间戳（毫秒级）
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static long GetTimestampMilliseconds(DateTime dateTime)
    {
        TimeSpan timeSpan = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return (long)timeSpan.TotalMilliseconds;
    }
       
    /// <summary>
    ///  将DateTime转换为时间戳（秒级）
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static long GetTimestampSeconds(DateTime dateTime)
    {
        TimeSpan timeSpan = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return (long)timeSpan.TotalSeconds;
    }


    // 获取当前日期时间
    public static DateTime GetCurrentDateTime()
    {
        return DateTime.Now;
    }
}
